<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>How do I attach data to some GTK+ object/widget?
[GTK 2.x]</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="GTK+ FAQ"
HREF="book1.html"><LINK
REL="UP"
TITLE="Development with GTK+: general questions"
HREF="c475.html"><LINK
REL="PREVIOUS"
TITLE="Why doesn't my widget (e.g. progressbar)
update? [GTK 2.x]"
HREF="x601.html"><LINK
REL="NEXT"
TITLE="How do I remove the data I have attached to an
object?"
HREF="x623.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>GTK+ FAQ</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x601.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Development with GTK+: general questions</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x623.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN610"
>How do I attach data to some GTK+ object/widget?
<I
CLASS="EMPHASIS"
>[GTK 2.x]</I
></A
></H1
><P
>First of all, the attached data is stored in the
object_data field of a GtkObject. The type of this field is
GData, which is defined in glib.h.  So you should read the
gdataset.c file in your glib source directory very
carefully.</P
><P
>There are two (easy) ways to attach some data to a gtk
object.  Using <TT
CLASS="LITERAL"
>g_object_set_data()</TT
> and
<TT
CLASS="LITERAL"
>g_object_get_data()</TT
> seems to be the most
common way to do this, as it provides a powerful interface to
connect objects and data.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>void g_object_set_data(GObject *object, const gchar *key, gpointer data);

gpointer g_object_get_data(GObject *object, const gchar *key);</PRE
></TD
></TR
></TABLE
><P
>Since a short example is better than any lengthy speech:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>struct my_struct	p1,p2,*result;
GtkWidget		*w;

g_object_set_data(G_OBJECT(w),"p1 data",(gpointer)&#38;p1);
g_object_set_data(G_OBJECT(w),"p2 data",(gpointer)&#38;p2);

result = g_object_get_data(G_OBJECT(w),"p1 data");</PRE
></TD
></TR
></TABLE
><P
>The <TT
CLASS="LITERAL"
>gtk_object_set_user_data()</TT
> and
<TT
CLASS="LITERAL"
>gtk_object_get_user_data()</TT
> functions does
exactly the same thing as the functions above, but does not
let you specify the "key" parameter.Instead, it uses a
standard "user_data" key. Note that the use of these functions
is deprecated in 1.2. They only provide a compatibility mode
with some old gtk packages.</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x601.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x623.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Why doesn't my widget (e.g. progressbar)
update? <I
CLASS="EMPHASIS"
>[GTK 2.x]</I
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c475.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>How do I remove the data I have attached to an
object?</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>